
package com.tpv.easychangelanguage;

import java.util.ArrayList;
import java.util.Locale;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class LanguageListAdapter extends BaseAdapter {
    private static final String TAG = "LanguageListAdapter";
    private LayoutInflater mInflater;
    private Context mContext;

    private ArrayList<LanguageInfo> mLanguageInfoArray = new ArrayList<LanguageInfo>();// mLanguageName//mLanguageAbbreviation

    public LanguageListAdapter(Context context) {
        this.mInflater = LayoutInflater.from(context);
        mContext = context;
        // Locale locale = context.getResources().getConfiguration().locale;
        // mCurrentSysLan = locale.getLanguage();
    }

    @Override
    public int getCount() {
        return mLanguageInfoArray == null ? 0 : mLanguageInfoArray.size();
    }

    @Override
    public Object getItem(int position) {
        return mLanguageInfoArray == null ? null : mLanguageInfoArray.get(position);
    }

    @Override
    public long getItemId(int arg0) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        Log.v(TAG, " getView() " + position + " " + convertView);

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.language_list_item, null);
            holder = new ViewHolder();
            /* get component object */
            holder.mLanguageName = (TextView) convertView.findViewById(R.id.language_name);
            holder.mLanguageAbbreviation = (TextView) convertView
                    .findViewById(R.id.language_abbreviation);
            convertView.setTag(holder);// bind ViewHolder object
        } else {
            holder = (ViewHolder) convertView.getTag(); // get ViewHolder object
        }

        if (holder != null) {
            if (mLanguageInfoArray != null && position < mLanguageInfoArray.size()
                    && mLanguageInfoArray.get(position) != null) {
                String lanAbb = mLanguageInfoArray.get(position).getLanguageAbbreviation();
                holder.mLanguageName.setText(mLanguageInfoArray.get(position).getLanguageName());
                holder.mLanguageAbbreviation.setText(lanAbb);

                if (isCurrentLanguage(lanAbb))
                    holder.mLanguageName.setTextColor(
                            mContext.getResources().getColor(R.color.current_language_color,
                                    mContext.getTheme()));
                else
                    holder.mLanguageName.setTextColor(
                            mContext.getResources().getColor(R.color.default_color,
                                    mContext.getTheme()));
            }
        }
        return convertView;
    }

    /**
     * the holder of place component
     */
    public final class ViewHolder {
        public TextView mLanguageName;
        public TextView mLanguageAbbreviation;
    }

    public void initLanguageInfoArray(ArrayList<LanguageInfo> languageInfoArray) {
        mLanguageInfoArray = languageInfoArray;
    }

    /**
     * current selected language is system language ?
     */
    private boolean isCurrentLanguage(String lanAbb) {
        Locale locale = mContext.getResources().getConfiguration().locale;
        String currentSysLan = locale.getLanguage();
        String country = locale.getCountry();
        if (!TextUtils.isEmpty(country))
            currentSysLan += "-" + country;
        if (!TextUtils.isEmpty(currentSysLan) && currentSysLan.equals(lanAbb))
            return true;
        else
            return false;
    }

    public ArrayList<LanguageInfo> getLanguageInfoArray() {
        return mLanguageInfoArray;
    }
}
